package com.puzzletest;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.puzzletest.bean.General;
import com.puzzletest.net.LoadData;
import com.puzzletest.net.Util;
import com.puzzletest.view.CustomImageView;

public class SingleActivity extends Activity {

	private CustomImageView imageView;
	private Button button;
	private Button recover;
	private final Activity activity = this;
	
	private ProgressDialog progress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_single);
		
		imageView  = (CustomImageView) findViewById(R.id.imageView);
		button = (Button) findViewById(R.id.button);
		recover = (Button) findViewById(R.id.recover);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showProgress();
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						
						PointF pointF = imageView.getCustomRotationPoint();
						float[] points = imageView.getCustomArrayPoint();
						String pointarray = "";
						for (float f : points) {
							pointarray += String.valueOf(f)+",";
						}
						String rotationpoint = pointF.x+","+pointF.y;
						String scale = String.valueOf(imageView.getCustomScale()); // 缩放比例
						String rotation = String.valueOf(imageView.getCustomRotation());//旋转的角度
						pointarray = pointarray.substring(0, pointarray.length() -1);
						
						General g = LoadData.putImageInfo(scale, rotation, rotationpoint, pointarray);
						Message msg = new Message();
						msg.obj = g;
						msg.what = 1;
						mHandler.sendMessage(msg);
						
					}
				}).start();
				
			}
		});
		
		recover.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, SingleRecoverActivity.class);
				startActivity(intent);
			}
		});
		
	}

	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				hiddenProgress();
				General g = (General) msg.obj;
				Toast.makeText(activity, g.getMessage(), Toast.LENGTH_SHORT).show();
				
				break;

			default:
				break;
			}
		};
	};
	
	/**
	 * 显示载入数据的提示信息
	 */
	private void showProgress() {
		if (progress == null) {
			progress = Util.show_process(activity,
					activity.getString(R.string.loading_data), false);
		}
		progress.show();
	}

	/**
	 * 隐藏载入数据的提示信息
	 */
	private void hiddenProgress() {
		if (progress != null) {
			progress.cancel();
			progress = null;
		}
	}
	
}
